<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Safe - Compile and execute code in restricted compartments - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/Safe">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Safe.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/Safe">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/Safe">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/Safe">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/Safe">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/Safe">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/Safe">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/Safe">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/Safe">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/Safe">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/Safe">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/Safe">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/Safe">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/Safe">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/Safe">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/Safe">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/Safe">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/Safe">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/Safe">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/Safe">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/Safe">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/Safe">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/Safe">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/Safe">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/Safe">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/Safe">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/Safe">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/Safe">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/Safe">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/Safe">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/Safe">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/Safe">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/Safe">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/Safe">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/Safe">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/Safe">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/Safe">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/Safe">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/Safe">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/Safe">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/Safe">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/Safe">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/Safe">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/Safe">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/Safe">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/Safe">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/Safe">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/Safe">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/Safe">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/Safe">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/Safe">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/Safe">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/Safe">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/Safe">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/Safe">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/Safe">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/Safe">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/Safe">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/Safe">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/Safe">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/Safe">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/Safe">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/Safe">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/Safe">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/Safe">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/Safe">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/Safe">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/Safe">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/Safe">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/Safe">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/Safe">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/Safe">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/Safe">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/Safe">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/Safe">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/Safe">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/Safe">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/Safe">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/Safe">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/Safe">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/Safe">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Safe.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/Safe">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/Safe">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/Safe">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/Safe">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/Safe">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/Safe">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/Safe">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/Safe">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/Safe">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/Safe">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/Safe">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/Safe">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/Safe">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/Safe">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/Safe">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/Safe">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/Safe">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/Safe">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/Safe">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/Safe">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/Safe">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/Safe">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/Safe">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/Safe">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/Safe">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/Safe">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/Safe">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/Safe">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/Safe">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/Safe">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/Safe">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/Safe">5.35.1</a>
          <a class="dropdown-item active" href="Safe.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="Safe.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="Safe.html">Safe</a>
          <div id="more">
            (<a href="Safe.txt">source</a>,
            <a href="https://metacpan.org/pod/Safe">CPAN</a>)
          </div>
            <div id="moduleversion">version 2.43</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="Safe.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#WARNING">WARNING</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#METHODS">METHODS</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="Safe.html#permit-(OP,-...)">permit (OP, ...)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#permit_only-(OP,-...)">permit_only (OP, ...)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#deny-(OP,-...)">deny (OP, ...)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#deny_only-(OP,-...)">deny_only (OP, ...)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#trap-(OP,-...),-untrap-(OP,-...)">trap (OP, ...), untrap (OP, ...)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#share-(NAME,-...)">share (NAME, ...)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#share_from-(PACKAGE,-ARRAYREF)">share_from (PACKAGE, ARRAYREF)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#varglob-(VARNAME)">varglob (VARNAME)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#reval-(STRING,-STRICT)">reval (STRING, STRICT)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#rdo-(FILENAME)">rdo (FILENAME)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#root-(NAMESPACE)">root (NAMESPACE)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#mask-(MASK)">mask (MASK)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#wrap_code_ref-(CODEREF)">wrap_code_ref (CODEREF)</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#wrap_code_refs_within-(...)">wrap_code_refs_within (...)</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#RISKS">RISKS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="Safe.html#AUTHOR">AUTHOR</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="Safe.html#NAME">#</a>NAME</h1>

<p>Safe - Compile and execute code in restricted compartments</p>

<h1 id="SYNOPSIS"><a class="permalink" href="Safe.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use Safe;

$compartment = new Safe;

$compartment-&gt;permit(qw(time sort :browse));

$result = $compartment-&gt;reval($unsafe_code);</code></pre>

<h1 id="DESCRIPTION"><a class="permalink" href="Safe.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>The Safe extension module allows the creation of compartments in which perl code can be evaluated. Each compartment has</p>

<dl>

<dt id="a-new-namespace"><a class="permalink" href="Safe.html#a-new-namespace">#</a><a id="a"></a>a new namespace</dt>
<dd>

<p>The &quot;root&quot; of the namespace (i.e. &quot;main::&quot;) is changed to a different package and code evaluated in the compartment cannot refer to variables outside this namespace, even with run-time glob lookups and other tricks.</p>

<p>Code which is compiled outside the compartment can choose to place variables into (or <i>share</i> variables with) the compartment&#39;s namespace and only that data will be visible to code evaluated in the compartment.</p>

<p>By default, the only variables shared with compartments are the &quot;underscore&quot; variables $_ and @_ (and, technically, the less frequently used %_, the _ filehandle and so on). This is because otherwise perl operators which default to $_ will not work and neither will the assignment of arguments to @_ on subroutine entry.</p>

</dd>
<dt id="an-operator-mask"><a class="permalink" href="Safe.html#an-operator-mask">#</a><a id="an"></a>an operator mask</dt>
<dd>

<p>Each compartment has an associated &quot;operator mask&quot;. Recall that perl code is compiled into an internal format before execution. Evaluating perl code (e.g. via &quot;eval&quot; or &quot;do &#39;file&#39;&quot;) causes the code to be compiled into an internal format and then, provided there was no error in the compilation, executed. Code evaluated in a compartment compiles subject to the compartment&#39;s operator mask. Attempting to evaluate code in a compartment which contains a masked operator will cause the compilation to fail with an error. The code will not be executed.</p>

<p>The default operator mask for a newly created compartment is the &#39;:default&#39; optag.</p>

<p>It is important that you read the <a href="Opcode.html">Opcode</a> module documentation for more information, especially for detailed definitions of opnames, optags and opsets.</p>

<p>Since it is only at the compilation stage that the operator mask applies, controlled access to potentially unsafe operations can be achieved by having a handle to a wrapper subroutine (written outside the compartment) placed into the compartment. For example,</p>

<pre><code>$cpt = new Safe;
sub wrapper {
  # vet arguments and perform potentially unsafe operations
}
$cpt-&gt;share(&#39;&amp;wrapper&#39;);</code></pre>

</dd>
</dl>

<h1 id="WARNING"><a class="permalink" href="Safe.html#WARNING">#</a>WARNING</h1>

<p>The Safe module does not implement an effective sandbox for evaluating untrusted code with the perl interpreter.</p>

<p>Bugs in the perl interpreter that could be abused to bypass Safe restrictions are not treated as vulnerabilities. See <a href="perlsecpolicy.html">perlsecpolicy</a> for additional information.</p>

<p>The authors make <b>no warranty</b>, implied or otherwise, about the suitability of this software for safety or security purposes.</p>

<p>The authors shall not in any case be liable for special, incidental, consequential, indirect or other similar damages arising from the use of this software.</p>

<p>Your mileage will vary. If in any doubt <b>do not use it</b>.</p>

<h1 id="METHODS"><a class="permalink" href="Safe.html#METHODS">#</a>METHODS</h1>

<p>To create a new compartment, use</p>

<pre><code>$cpt = new Safe;</code></pre>

<p>Optional argument is (NAMESPACE), where NAMESPACE is the root namespace to use for the compartment (defaults to &quot;Safe::Root0&quot;, incremented for each new compartment).</p>

<p>Note that version 1.00 of the Safe module supported a second optional parameter, MASK. That functionality has been withdrawn pending deeper consideration. Use the permit and deny methods described below.</p>

<p>The following methods can then be used on the compartment object returned by the above constructor. The object argument is implicit in each case.</p>

<h2 id="permit-(OP,-...)"><a class="permalink" href="Safe.html#permit-(OP,-...)">#</a><a id="permit"></a><a id="permit-OP"></a>permit (OP, ...)</h2>

<p>Permit the listed operators to be used when compiling code in the compartment (in <i>addition</i> to any operators already permitted).</p>

<p>You can list opcodes by names, or use a tag name; see <a href="Opcode.html#Predefined-Opcode-Tags">&quot;Predefined Opcode Tags&quot; in Opcode</a>.</p>

<h2 id="permit_only-(OP,-...)"><a class="permalink" href="Safe.html#permit_only-(OP,-...)">#</a><a id="permit_only"></a><a id="permit_only-OP"></a>permit_only (OP, ...)</h2>

<p>Permit <i>only</i> the listed operators to be used when compiling code in the compartment (<i>no</i> other operators are permitted).</p>

<h2 id="deny-(OP,-...)"><a class="permalink" href="Safe.html#deny-(OP,-...)">#</a><a id="deny"></a><a id="deny-OP"></a>deny (OP, ...)</h2>

<p>Deny the listed operators from being used when compiling code in the compartment (other operators may still be permitted).</p>

<h2 id="deny_only-(OP,-...)"><a class="permalink" href="Safe.html#deny_only-(OP,-...)">#</a><a id="deny_only"></a><a id="deny_only-OP"></a>deny_only (OP, ...)</h2>

<p>Deny <i>only</i> the listed operators from being used when compiling code in the compartment (<i>all</i> other operators will be permitted, so you probably don&#39;t want to use this method).</p>

<h2 id="trap-(OP,-...),-untrap-(OP,-...)"><a class="permalink" href="Safe.html#trap-(OP,-...),-untrap-(OP,-...)">#</a><a id="trap"></a><a id="trap-OP-...-untrap-OP"></a>trap (OP, ...), untrap (OP, ...)</h2>

<p>The trap and untrap methods are synonyms for deny and permit respectfully.</p>

<h2 id="share-(NAME,-...)"><a class="permalink" href="Safe.html#share-(NAME,-...)">#</a><a id="share"></a><a id="share-NAME"></a>share (NAME, ...)</h2>

<p>This shares the variable(s) in the argument list with the compartment. This is almost identical to exporting variables using the <a href="Exporter.html">Exporter</a> module.</p>

<p>Each NAME must be the <b>name</b> of a non-lexical variable, typically with the leading type identifier included. A bareword is treated as a function name.</p>

<p>Examples of legal names are &#39;$foo&#39; for a scalar, &#39;@foo&#39; for an array, &#39;%foo&#39; for a hash, &#39;&amp;foo&#39; or &#39;foo&#39; for a subroutine and &#39;*foo&#39; for a glob (i.e. all symbol table entries associated with &quot;foo&quot;, including scalar, array, hash, sub and filehandle).</p>

<p>Each NAME is assumed to be in the calling package. See share_from for an alternative method (which <code>share</code> uses).</p>

<h2 id="share_from-(PACKAGE,-ARRAYREF)"><a class="permalink" href="Safe.html#share_from-(PACKAGE,-ARRAYREF)">#</a><a id="share_from"></a><a id="share_from-PACKAGE-ARRAYREF"></a>share_from (PACKAGE, ARRAYREF)</h2>

<p>This method is similar to share() but allows you to explicitly name the package that symbols should be shared from. The symbol names (including type characters) are supplied as an array reference.</p>

<pre><code>$safe-&gt;share_from(&#39;main&#39;, [ &#39;$foo&#39;, &#39;%bar&#39;, &#39;func&#39; ]);</code></pre>

<p>Names can include package names, which are relative to the specified PACKAGE. So these two calls have the same effect:</p>

<pre><code>$safe-&gt;share_from(&#39;Scalar::Util&#39;, [ &#39;reftype&#39; ]);
$safe-&gt;share_from(&#39;main&#39;, [ &#39;Scalar::Util::reftype&#39; ]);</code></pre>

<h2 id="varglob-(VARNAME)"><a class="permalink" href="Safe.html#varglob-(VARNAME)">#</a><a id="varglob"></a><a id="varglob-VARNAME"></a>varglob (VARNAME)</h2>

<p>This returns a glob reference for the symbol table entry of VARNAME in the package of the compartment. VARNAME must be the <b>name</b> of a variable without any leading type marker. For example:</p>

<pre><code>${$cpt-&gt;varglob(&#39;foo&#39;)} = &quot;Hello world&quot;;</code></pre>

<p>has the same effect as:</p>

<pre><code>$cpt = new Safe &#39;Root&#39;;
$Root::foo = &quot;Hello world&quot;;</code></pre>

<p>but avoids the need to know $cpt&#39;s package name.</p>

<h2 id="reval-(STRING,-STRICT)"><a class="permalink" href="Safe.html#reval-(STRING,-STRICT)">#</a><a id="reval"></a><a id="reval-STRING-STRICT"></a>reval (STRING, STRICT)</h2>

<p>This evaluates STRING as perl code inside the compartment.</p>

<p>The code can only see the compartment&#39;s namespace (as returned by the <b>root</b> method). The compartment&#39;s root package appears to be the <code>main::</code> package to the code inside the compartment.</p>

<p>Any attempt by the code in STRING to use an operator which is not permitted by the compartment will cause an error (at run-time of the main program but at compile-time for the code in STRING). The error is of the form &quot;&#39;%s&#39; trapped by operation mask...&quot;.</p>

<p>If an operation is trapped in this way, then the code in STRING will not be executed. If such a trapped operation occurs or any other compile-time or return error, then $@ is set to the error message, just as with an eval().</p>

<p>If there is no error, then the method returns the value of the last expression evaluated, or a return statement may be used, just as with subroutines and <b>eval()</b>. The context (list or scalar) is determined by the caller as usual.</p>

<p>If the return value of reval() is (or contains) any code reference, those code references are wrapped to be themselves executed always in the compartment. See <a href="Safe.html#wrap_code_refs_within">&quot;wrap_code_refs_within&quot;</a>.</p>

<p>The formerly undocumented STRICT argument sets strictness: if true &#39;use strict;&#39; is used, otherwise it uses &#39;no strict;&#39;. <b>Note</b>: if STRICT is omitted &#39;no strict;&#39; is the default.</p>

<p>Some points to note:</p>

<p>If the entereval op is permitted then the code can use eval &quot;...&quot; to &#39;hide&#39; code which might use denied ops. This is not a major problem since when the code tries to execute the eval it will fail because the opmask is still in effect. However this technique would allow clever, and possibly harmful, code to &#39;probe&#39; the boundaries of what is possible.</p>

<p>Any string eval which is executed by code executing in a compartment, or by code called from code executing in a compartment, will be eval&#39;d in the namespace of the compartment. This is potentially a serious problem.</p>

<p>Consider a function foo() in package pkg compiled outside a compartment but shared with it. Assume the compartment has a root package called &#39;Root&#39;. If foo() contains an eval statement like eval &#39;$foo = 1&#39; then, normally, $pkg::foo will be set to 1. If foo() is called from the compartment (by whatever means) then instead of setting $pkg::foo, the eval will actually set $Root::pkg::foo.</p>

<p>This can easily be demonstrated by using a module, such as the Socket module, which uses eval &quot;...&quot; as part of an AUTOLOAD function. You can &#39;use&#39; the module outside the compartment and share an (autoloaded) function with the compartment. If an autoload is triggered by code in the compartment, or by any code anywhere that is called by any means from the compartment, then the eval in the Socket module&#39;s AUTOLOAD function happens in the namespace of the compartment. Any variables created or used by the eval&#39;d code are now under the control of the code in the compartment.</p>

<p>A similar effect applies to <i>all</i> runtime symbol lookups in code called from a compartment but not compiled within it.</p>

<h2 id="rdo-(FILENAME)"><a class="permalink" href="Safe.html#rdo-(FILENAME)">#</a><a id="rdo"></a><a id="rdo-FILENAME"></a>rdo (FILENAME)</h2>

<p>This evaluates the contents of file FILENAME inside the compartment. It uses the same rules as perl&#39;s built-in <code>do</code> to locate the file, poossibly using <code>@INC</code>.</p>

<p>See above documentation on the <b>reval</b> method for further details.</p>

<h2 id="root-(NAMESPACE)"><a class="permalink" href="Safe.html#root-(NAMESPACE)">#</a><a id="root"></a><a id="root-NAMESPACE"></a>root (NAMESPACE)</h2>

<p>This method returns the name of the package that is the root of the compartment&#39;s namespace.</p>

<p>Note that this behaviour differs from version 1.00 of the Safe module where the root module could be used to change the namespace. That functionality has been withdrawn pending deeper consideration.</p>

<h2 id="mask-(MASK)"><a class="permalink" href="Safe.html#mask-(MASK)">#</a><a id="mask"></a><a id="mask-MASK"></a>mask (MASK)</h2>

<p>This is a get-or-set method for the compartment&#39;s operator mask.</p>

<p>With no MASK argument present, it returns the current operator mask of the compartment.</p>

<p>With the MASK argument present, it sets the operator mask for the compartment (equivalent to calling the deny_only method).</p>

<h2 id="wrap_code_ref-(CODEREF)"><a class="permalink" href="Safe.html#wrap_code_ref-(CODEREF)">#</a><a id="wrap_code_ref"></a><a id="wrap_code_ref-CODEREF"></a>wrap_code_ref (CODEREF)</h2>

<p>Returns a reference to an anonymous subroutine that, when executed, will call CODEREF with the Safe compartment &#39;in effect&#39;. In other words, with the package namespace adjusted and the opmask enabled.</p>

<p>Note that the opmask doesn&#39;t affect the already compiled code, it only affects any <i>further</i> compilation that the already compiled code may try to perform.</p>

<p>This is particularly useful when applied to code references returned from reval().</p>

<p>(It also provides a kind of workaround for RT#60374: &quot;Safe.pm sort {} bug with -Dusethreads&quot;. See <a href="https://rt.perl.org/rt3//Public/Bug/Display.html?id=60374">https://rt.perl.org/rt3//Public/Bug/Display.html?id=60374</a> for <i>much</i> more detail.)</p>

<h2 id="wrap_code_refs_within-(...)"><a class="permalink" href="Safe.html#wrap_code_refs_within-(...)">#</a><a id="wrap_code_refs_within"></a>wrap_code_refs_within (...)</h2>

<p>Wraps any CODE references found within the arguments by replacing each with the result of calling <a href="Safe.html#wrap_code_ref">&quot;wrap_code_ref&quot;</a> on the CODE reference. Any ARRAY or HASH references in the arguments are inspected recursively.</p>

<p>Returns nothing.</p>

<h1 id="RISKS"><a class="permalink" href="Safe.html#RISKS">#</a>RISKS</h1>

<p>This section is just an outline of some of the things code in a compartment might do (intentionally or unintentionally) which can have an effect outside the compartment.</p>

<dl>

<dt id="Memory"><a class="permalink" href="Safe.html#Memory">#</a>Memory</dt>
<dd>

<p>Consuming all (or nearly all) available memory.</p>

</dd>
<dt id="CPU"><a class="permalink" href="Safe.html#CPU">#</a>CPU</dt>
<dd>

<p>Causing infinite loops etc.</p>

</dd>
<dt id="Snooping"><a class="permalink" href="Safe.html#Snooping">#</a>Snooping</dt>
<dd>

<p>Copying private information out of your system. Even something as simple as your user name is of value to others. Much useful information could be gleaned from your environment variables for example.</p>

</dd>
<dt id="Signals"><a class="permalink" href="Safe.html#Signals">#</a>Signals</dt>
<dd>

<p>Causing signals (especially SIGFPE and SIGALARM) to affect your process.</p>

<p>Setting up a signal handler will need to be carefully considered and controlled. What mask is in effect when a signal handler gets called? If a user can get an imported function to get an exception and call the user&#39;s signal handler, does that user&#39;s restricted mask get re-instated before the handler is called? Does an imported handler get called with its original mask or the user&#39;s one?</p>

</dd>
<dt id="State-Changes"><a class="permalink" href="Safe.html#State-Changes">#</a><a id="State"></a>State Changes</dt>
<dd>

<p>Ops such as chdir obviously effect the process as a whole and not just the code in the compartment. Ops such as rand and srand have a similar but more subtle effect.</p>

</dd>
</dl>

<h1 id="AUTHOR"><a class="permalink" href="Safe.html#AUTHOR">#</a>AUTHOR</h1>

<p>Originally designed and implemented by Malcolm Beattie.</p>

<p>Reworked to use the Opcode module and other changes added by Tim Bunce.</p>

<p>Currently maintained by the Perl 5 Porters, &lt;perl5-porters@perl.org&gt;.</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
